package com.group01.web.controller;

import com.group01.dto.User.UserInsertInfoDTO;
import com.group01.dto.User.UserUpdateInfoDTO;
import com.group01.dto.User.UserUpdateStatusDTO;
import com.group01.enums.CodeAndMessageEnum;
import com.group01.pojo.User;
import com.group01.result.PageTemplate;
import com.group01.result.Result;
import com.group01.service.UserService;
import com.group01.vo.User.UserGetInfoByIdVO;
import com.group01.vo.User.UserGetPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.URL;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户层
 * @Description
 * @Author 翟玉龙
 * @Date 2024/6/20
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
@Validated
public class UserController {

    /**
     * 注入service
     */
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }


    /**
     * 分页+条件查询用户信息
     * @param pageNum   当前页码
     * @param pageSize  每页大小
     * @param username  用户名
     * @param status    用户状态
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return {@link Result }<{@link List }<{@link User }>>
     */
    @ApiOperation("分页+条件查询用户信息")
    @GetMapping("/getPageUser")
    public Result<PageTemplate<UserGetPageVO>> getPageUser(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime
    ) {
        PageTemplate<UserGetPageVO> pageTemplate = userService.getPage(pageNum, pageSize , username , status , startTime , endTime);
        return Result.success(CodeAndMessageEnum.FIND_RESULT , pageTemplate);
    }

    /**
     * 获取用户信息
     * @return {@link Result }<{@link UserGetInfoByIdVO }>
     */
    @GetMapping("/getUserInfo")
    public Result<UserGetInfoByIdVO> getUserInfo() {
        return userService.getUserInfo();
    }

    /**
     * 根据用户ID获取用户信息
     * 数据回显接口
     * @param id 用户id
     * @return {@link Result }<{@link User }>
     */
    @ApiOperation("根据用户ID获取用户信息")
    @GetMapping("/getUserInfoById/{id}")
    public Result<UserGetInfoByIdVO> getUserInfoById(@PathVariable("id") Integer id){
        return Result.success(CodeAndMessageEnum.FIND_RESULT, userService.getUserById(id));
    }

    /**
     * 新增用户
     * @param userInsertInfoDTO 添加用户传输层DTO对象类
     * @return {@link Result }<{@link User }>
     */
    @ApiOperation("新增用户")
    @PostMapping("/addUser")
    public Result<String> addUser(@RequestBody @Validated UserInsertInfoDTO userInsertInfoDTO){
        return userService.addUser(userInsertInfoDTO);
    }

    /**
     * 修改用户账号状态
     * @param userUpdateStatusDTO 修改用户状态传输对象DTO
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("修改用户账号状态")
    @PutMapping("/updateUserStatus")
    public Result<String> updateUserStatus(@RequestBody @Validated UserUpdateStatusDTO userUpdateStatusDTO){
        return userService.updateUserStatus(userUpdateStatusDTO);
    }

    /**
     * 修改用户信息
     * @param updateInfoDTO 传输对象dto
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("修改用户信息")
    @PutMapping("/updateUserInfo")
    public Result<UserGetInfoByIdVO> updateUserInfo(
            @Validated @RequestBody UserUpdateInfoDTO updateInfoDTO
    ) {
        return userService.updateUserInfo(updateInfoDTO);
    }

    /**
     * 删除与批量删除用户信息
     * @param ids 需要删除的用户id值
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("删除与批量删除用户信息")
    @DeleteMapping("/deleteUser/{ids}")
    public Result<String> deleteUser(@PathVariable("ids") String ids){
        return userService.deleteUsersByIds(ids);
    }
}
